package com.walkline.weibo.dao;

import java.util.Vector;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import com.walkline.util.Function;
import com.walkline.weibo.WeiboException;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Status;
import com.walkline.weibo.inf.StatusWrapper;

public class WeiboStatusWrapper extends WeiboObject implements StatusWrapper
{
	private int _total_number = 0;
	private Vector _statuses = new Vector();

	public WeiboStatusWrapper(WeiboSDK weibo, JSONObject jsonObject) throws WeiboException
	{
		super(weibo, jsonObject);

		JSONObject statusWrapper = jsonObject;
		if (statusWrapper != null)
		{
			_total_number = statusWrapper.optInt("total_number");

			JSONArray statuses = statusWrapper.optJSONArray("statuses");
			if (statuses != null)
			{
				JSONObject statusObject;
				Status status;
				boolean needTrimAD = (statuses.length() % 10) != 0;

				for (int i=0; i<statuses.length(); i++)
				{
					if (needTrimAD && i == 2) {_total_number -= 1; continue;}

					try {
						statusObject = (JSONObject) statuses.get(i);

						status = new WeiboStatus(weibo, statusObject);
						if (status != null) {_statuses.addElement(status);}
					} catch (JSONException e) {Function.errorDialog(e.toString());}
				}
			}
		}
	}

	public int getTotalNumber() {return _total_number;}

	public Vector getStatuses() {return _statuses;}
}